using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Elementum.GameEle.Draw;

namespace Elementum
{
    public class MainMenu : VisibleObject
    {
        protected Stage _stage;
        protected MenuItem _newGame, _continue, _volumn, _quit, vAdd, vSub, vBlock;
        VolumeBar vb;
        Boolean isVolume = false;


        public MainMenu(Stage stage)
        {
            this._stage = stage;
            string menuPath = "images/system/menu";

            InitializeItem(stage, menuPath);
        }

        protected virtual void InitializeItem(Stage stage, string menuPath)
        {
            _newGame = new MenuItem(stage, new Animation(menuPath, new Rectangle(2, 2, 264, 66), new Point(264, 66)));
            _newGame.Position = new Vector2(50 + 264 / 2, 80);
            _continue = new MenuItem(stage, new Animation(menuPath, new Rectangle(268, 2, 226, 66), new Point(226, 66)));
            _continue.Position = new Vector2(50 + 226 / 2, 160);
            _volumn = new MenuItem(stage, new Animation(menuPath, new Rectangle(234, 79, 190, 68), new Point(190, 68)));
            _volumn.Position = new Vector2(50 + 190 / 2, 240);
            _quit = new MenuItem(stage, new Animation(menuPath, new Rectangle(2, 138, 106, 66), new Point(106, 66)));
            _quit.Position = new Vector2(50 + 106 / 2, 320);

            vAdd = new MenuItem(stage, new Animation(menuPath, new Rectangle(129, 204, 50, 50), new Point(38, 37)));
            vAdd.Position = new Vector2(280 + 320-10, 240);

            vSub = new MenuItem(stage, new Animation(menuPath, new Rectangle(206, 214,50, 50), new Point(38, 37)));
            vSub.Position = new Vector2(280, 240 + 10);

            vb = new VolumeBar();
            vb.Position = new Vector2(320+60,240);

            _children.Add(_continue);
            _children.Add(_newGame);
            _children.Add(_volumn);
            _children.Add(_quit);


            if (StageManager.HasSave)
                _continue.TouchStateAction += MenuAction;
            else
                _continue.Alpha = 0.5f;

            _newGame.TouchStateAction += MenuAction;
            _volumn.TouchStateAction += MenuAction;
            _quit.TouchStateAction += MenuAction;

            vAdd.TouchStateAction+=VolumeMenuAction;
            vSub.TouchStateAction+=VolumeMenuAction;

        }

        protected void Continue()
        {
            StageManager.Initialize();
        }

        public virtual void MenuAction(VisibleObject pressedObject, TouchLocationState state)
        {
            if (state == TouchLocationState.Pressed)
            {
                if (pressedObject == _volumn)
                {
                    Volumn();
                }
                else if (!isVolume)
                    {
                        Transition tra = new DefauldTransition(0, 0, 10, 80);
                        if (pressedObject == _newGame)
                            tra.TransitionDoneAction += NewGame;
                        if (pressedObject == _continue)
                            tra.TransitionDoneAction += Continue;
                        if (pressedObject == _quit)
                        {
                            tra.TransitionDoneAction += Quit;
                        }

                        tra.Register(_stage);
                        tra = new DefauldTransition(0, 1, 5, 70);
                        tra.Register(_stage);
                        tra = new DefauldTransition(1, 1, 65, 85);
                        tra.Register(_stage);
                        PressMotion(pressedObject);
                    }

            }
        }

        public virtual void VolumeMenuAction(VisibleObject pressedObject, TouchLocationState state) {
            if (state == TouchLocationState.Pressed)
            {
                if (pressedObject == vAdd)
                {
                    vb.Value += 1;
                }
                if (pressedObject == vSub)
                {
                    vb.Value -= 1;
                }
            }
        }

        protected void PressMotion(VisibleObject pressedObject)
        {
            Vector2 pos = pressedObject.Position;
            PositionModifier pmv = new PositionModifier(pos, pos + new Vector2(150, 0), 3, 85);
            ModifierContain mpv = new ModifierContain(pressedObject);
            mpv.AddModifier(pmv);
            pressedObject.ModifierContainer = mpv;

            for (int i = 0; i < _children.Count(); i++)
            {
                MenuItem m = (MenuItem)_children[i];
                if (m != pressedObject)
                {
                    PositionModifier pm = null;
                    if (m.Position.Y < pressedObject.Position.Y) //upper than pressed
                        pm = new PositionModifier(m.Position, m.Position + new Vector2(0, -420), 0, 15);
                    if (m.Position.Y > pressedObject.Position.Y)//downer than pressed
                        pm = new PositionModifier(m.Position, m.Position + new Vector2(0, 420), 0, 15);
                    ModifierContain mc = new ModifierContain(m);
                    pm.ModifyDoneAction += pm.UnRegister;
                    mc.AddModifier(pm);
                    m.ModifierContainer = mc;
                }

            }
        }

        protected void NewGame()
        {
            StageManager.Save = new DataNeedSave();
            GameClass._firstInGame = true;
            StageManager.ProtalCommand = new ProtalCommand("rootStage", "rootProtal");
        }

        protected void Quit()
        {
            GameClass.IsExit = true;
            Transition tra = new DefauldTransition(1, 1, 0, 5);
            tra.Register(_stage);
        }

        protected void Volumn()
        {
            isVolume = !isVolume;
            if (isVolume)
            {
                _children.Add(vAdd);
                _children.Add(vSub);
                _children.Add(vb);
            }
            else
            {
                _children.Remove(vAdd);
                _children.Remove(vSub);
                _children.Remove(vb);
            }
        }

        public override void OnDraw(SpriteBatch spriteBatch)
        {
            base.OnDraw(spriteBatch);
        }

        public override void Update(int delta)
        {
            base.Update(delta);
        }

    }
}
